home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 June: Reference Library / Dev.CD Jun 99 RL Disk 1.toast / Technical Documentation / Develop / Additional Articles / Developing Symbiotic Apps / Symbiotic Samples / Symbiotic server source / Fractal Daemon / mandel.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-13  |  4.5 KB  |  217 lines  |  [TEXT/CWIE]

  1. /* mandel.c -- main routine for Mandelbrot UNIX daemon.
  2.  *
  3.  * %W%
  4.  *
  5.  * Author: Scott Mulligan
  6.  * Copyright 1993,1994 Apple Computer, Inc.
  7.  * All Rights Reserved.
  8.  *
  9.  * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF APPLE COMPUTER, INC.
  10.  * The copyright notice above does not evidence any actual or
  11.  * intended publication of such source code.
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <netdb.h>    /* */
  16. #include <sys/errno.h>    /* */
  17. /* #include <sys/id.h>    /* */
  18. #include <sys/types.h>    /* */
  19. #include <sys/socket.h>    /* */
  20. #include <netinet/in.h>    /* */
  21. /* #include <pwd.h>    /* */
  22. /* #include <unistd.h>    /* */
  23. /* #include <sys/access.h>    /* */
  24.  
  25. #include "Types.h"
  26. #include "AppleEvents.h"
  27. #include "AUXAESuite.h"
  28. #include "mandel.h"
  29. /* #include "UAppleEvents.h" */
  30. #include <sys/file.h>
  31. #include <sys/wait.h>
  32. #include <signal.h>    /* */
  33.  
  34. /* Global variable declarations */
  35.  
  36. AEAddressDesc target;
  37.  
  38. int debuglevel=0;
  39. int sessionid=0;
  40. int missed_heartbeats=0;
  41. int theInterval=0;
  42. boolean sendconfig=FALSE;
  43. boolean TimeToQuit=FALSE;
  44. boolean pending_cmd=FALSE;
  45. char qcmds[4][128];
  46. int ncmds=0;
  47.  
  48. char *databuf;
  49. char OSVersion[32];
  50. char *clientVersion;
  51. char *serverVersion;
  52.  
  53. time_t time_of_last_recv;    /* last heartbeat received                       */
  54. time_t time_of_last_check;    /* last time we checked how long since we recv'd */
  55. time_t time_of_last_send;    /* last time we sent a heartbeat                 */
  56. time_t time_of_last_update;    /* last time we sent an update                   */
  57.  
  58. FILE *debugfp;
  59.  
  60.  
  61.  
  62. /* External variable declarations */
  63.  
  64. extern int errno;
  65.  
  66.  
  67.  
  68. /* The AEHandler structure */
  69.  
  70. typedef struct _AEHandler {
  71.     AEEventID theAEEventID;
  72.     AEEventHandlerProcPtr handler;
  73. } AEHandler;
  74.  
  75.  
  76.  
  77. /* The "AppleEvents" handler table */
  78.  
  79. AEHandler handlerTable[] = {
  80.     { kAENull,
  81.         HandleNullEvent },
  82.     { kAEQuit,
  83.         HandleQuit },
  84.     { kAEHeartBeat,
  85.         HandleHeartBeat },
  86.     { kAEVersion,
  87.         HandleVersionEvent },
  88.     { kAEMandel,
  89.         HandleMandel }
  90. };
  91.  
  92.  
  93.  
  94.  
  95. void setup_handler_tbl()
  96. {
  97. int i, result;
  98.  
  99.     AEInit();
  100.  
  101.     for (i = 0;  i < sizeof(handlerTable) / sizeof(AEHandler); i++) {
  102.     result = AEInstallEventHandler(kAEAUXSuite, 
  103.                    handlerTable[i].theAEEventID,
  104.                    handlerTable[i].handler,
  105.                    0L, false);
  106.     }
  107.  
  108. }
  109.  
  110.  
  111.  
  112. reap_child()
  113. {
  114. int status, pid;
  115.  
  116. #ifdef DEBUG
  117.     fprintf(debugfp,"reap_child(): At the top.\n");
  118.     fflush(debugfp);
  119. #endif
  120.     pid=wait3(&status, WNOHANG,0);
  121. #ifdef DEBUG
  122.     fprintf(debugfp,"reap_child(): At the bottom.\n");
  123.     fflush(debugfp);
  124. #endif
  125.     return(0);
  126. }
  127.  
  128.  
  129. /*
  130.  * This is the main() routine for mandeld.  All of the daemon startup work
  131.  * has already been done for us by ppcd following the standard UNIX
  132.  * inetd model.
  133.  *
  134.  * We inherit file descriptor 0 from ppcd.  It is an active TCP socket
  135.  * connection to the client application.
  136.  */
  137.  
  138. void main(argc, argv)
  139. int argc;
  140. char** argv;
  141. {
  142. OSErr result;
  143. EventRecord event;
  144. int i,num,time_out;
  145. int six_oh_eight=0;
  146. time_t time_of_select, time_now;
  147.  
  148.     chdir ("/tmp");    /* so core files end up in /tmp */
  149.  
  150.     setup_debug(argv[0]);
  151.  
  152.     set_access(argc,argv);
  153.  
  154.     setup_handler_tbl(v);
  155.  
  156.     signal(SIGCHLD,reap_child);
  157.  
  158.     time_of_last_recv = 0;
  159.     time_of_last_send = 0;
  160.     time_of_last_check = 0;
  161.     time_of_last_update = 0;
  162.  
  163.  
  164. #ifdef SLEEP
  165.     #ifdef DEBUG
  166.         fprintf(debugfp,"MANDELD: Sleeping 60 seconds to allow gdb attach...\n");
  167.     #endif
  168.     sleep(60);
  169. #endif
  170.  
  171.  
  172.     (void)time(&time_of_last_recv);
  173.     (void)time(&time_of_last_send);
  174.  
  175.  
  176.     for (;;) {
  177.  
  178.     (void)time(&time_now);
  179.     if ((time_of_last_check + HEARTBEAT_CHECK - time_now) <
  180.         (time_of_last_send + HEARTBEAT_SEND - time_now))
  181.             time_out = time_of_last_check + HEARTBEAT_CHECK - time_now; 
  182.     else
  183.             time_out = time_of_last_send + HEARTBEAT_SEND - time_now; 
  184.  
  185.     if (theInterval > 0)
  186.         if ((time_of_last_update + theInterval - time_now) < time_out)
  187.                 time_out = time_of_last_update + theInterval - time_now; 
  188.  
  189.         if (time_out <= 0) time_out = 1;
  190.  
  191.         result = WaitNextAppleEvent (&event, &time_out);
  192.     if (result == -1) {
  193. #ifdef DEBUG
  194.         fprintf (debugfp,"main(): Looks like the client quit...\n");
  195.         fflush(debugfp);
  196. #endif
  197.             TimeToQuit = TRUE;
  198.         }
  199.     if (result > 0)
  200.             result = AEProcessAppleEvent (&event);
  201.  
  202.     if (TimeToQuit == TRUE) shutdown_now();
  203.     (void)time(&time_now);
  204.     if ((time_now - time_of_last_send ) >= HEARTBEAT_SEND ) snd_heartbeat();
  205.     if ((time_now - time_of_last_check) >= HEARTBEAT_CHECK) chk_heartbeat();
  206.  
  207. #ifdef DEBUG
  208.     fprintf (debugfp,"main(): Bottom of main loop.\n");
  209.     fflush(debugfp);
  210. #endif
  211.     }
  212.  
  213.     exit(0);
  214. }
  215.  
  216. /*** EOF ***/
  217.